<template>
  <view class="work-info-detail">
    <!-- 使用通用展示组件（只读模式） -->
    <DetailDisplayList
      :config="config"
      :list="workList"
      :loading="loading"
      :show-actions="false"
    />
  </view>
</template>

<script setup lang="ts">
import { ref, watch } from "vue";
import SysHrInterviewRequestBillApi from "../api/index";
import type { IWorkResponse, ISubTableSearchRequest } from "../api/interface";
import DetailDisplayList from "./DetailDisplayList.vue";
import { workDisplayConfig } from "./detailDisplayConfig";

const api = SysHrInterviewRequestBillApi();

// Props
interface Props {
  personId?: string;
  readonly?: boolean;
}

const props = withDefaults(defineProps<Props>(), {
  personId: "",
  readonly: false,
});

// 数据
const loading = ref(false);
const workList = ref<IWorkResponse[]>([]);

// 详情展示配置（只读模式）
const config = ref({
  ...workDisplayConfig,
  addButtonText: "",
});

// 加载工作经历列表
const loadWorkList = async () => {
  if (!props.personId) {
    return;
  }

  loading.value = true;

  try {
    const requestData: ISubTableSearchRequest = {
      Limit: 50,
      Page: 1,
      PersonId: props.personId,
      SortInfo: null,
    };

    const res = await api.getWorkList(requestData);

    if (res && res.Data && Array.isArray(res.Data)) {
      workList.value = res.Data;
      console.log("工作经历组件：获取工作经历成功:", workList.value);
      if (workList.value.length > 0) {
        console.log("工作经历组件：第一条数据结构:", workList.value[0]);
      }
    } else {
      workList.value = [];
    }
  } catch (error) {
    console.error("工作信息组件：获取工作经历异常:", error);
    workList.value = [];
    uni.showToast({
      title: "获取工作经历失败",
      icon: "none",
    });
  } finally {
    loading.value = false;
  }
};

// 监听personId变化，加载工作经历
watch(
  () => props.personId,
  (newId) => {
    if (newId) {
      loadWorkList();
    }
  },
  { immediate: true },
);

// 暴露方法供父组件调用
defineExpose({
  loadWorkList,
});
</script>

<!-- 工作经历组件不需要自定义样式，完全使用通用组件 -->
